<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-29
 * Time: 13:48
 */

namespace backend\controllers;
use app\models\AuthItemChild;
use app\models\CrmFrontPermission;
use app\models\CrmSystemGsxx;
use mdm\admin\components\Configs;
use mdm\admin\components\Helper;
use mdm\admin\models\AuthItem;
use mdm\admin\models\Route;
use mdm\admin\models\searchs\AuthItem as AuthItemSearch;
use yii\base\Object;
use yii\helpers\ArrayHelper;
use yii\rbac\Item;

class QxSysnicController extends CommonController
{

    public function actionSysnic()
    {
        $permission = new CrmFrontPermission();
        $one = $permission->find()->select('*')->one();
        if ($one){//有
            $one->permission = json_decode($_POST['nav']);
            $one->update();
        }else{
            $permission->permission = json_decode($_POST['nav']);
            $permission->save();
        }
       return $this->dealFD('同步成功！',1);
    }

    public function actionAllPermission()
    {
        $permission = new CrmFrontPermission();
        $one = $permission->find()->select('*')->one();
        return $this->dealFD(ArrayHelper::toArray($one),1);
    }

    public function actionSysnic_()
    {
        $this->startT();
        if (isset($_POST['id'])){
            $one = CrmSystemGsxx::findOne(['id'=>$_POST['id']]);
        }else{
            $one = CrmSystemGsxx::findOne(['pid'=>'0']);
        }
        $changeNav = json_decode($_POST['changeNav']);
        $parentShow = true;
        if ($changeNav->show==false){//删除权限，其子都要改成没有权限
            $one->permission = array_values(array_filter($one->permission,function($i)use($changeNav){
                return $i!= [$changeNav->name=>""];
            }));
            $this->findAllChildren($one->id,$res);
            if ($res!=null && count($res)>0){
                foreach ($res as $k=>$v){//循环所有的子
                    if ($v->permission){//循环所有的权限
                        $v->permission = array_values(array_filter($v->permission,function($i)use($changeNav){
                            return $i!= [$changeNav->name=>""];
                        }));
                        $v->update();
                    }
                }
            }
        }else{//新增权限，其父要判断权限，没有权限就不能新增
            if ($one->permission==null){
                $one->permission = [[$changeNav->name=>""]];
            }else{
                $one->permission = array_values(array_merge($one->permission,[[$changeNav->name=>""]]));
            }
            $this->findAllParents($one->pid,$res);
            if ($res!=null && count($res)>0) {
                foreach ($res as $k => $v) {//循环所有的父
                    if ($v->permission){//循环所有的权限
                        $permission = array_values(array_filter($v->permission,function($i)use($changeNav){
                            return $i== [$changeNav->name=>""];
                        }));
                        if (count($permission)>0){
                            $parentShow = $parentShow && true;
                        }else{
                            $parentShow = $parentShow && false;
                        }
                    }
                }
            }
        }
        if ($parentShow){
            if ($one->update()) {
                $this->transaction->commit();
                return $this->dealFD('保存成功！',1);
            } else {
                return $this->dealFD('保存失败！',0);
            }
        }else{
            return $this->dealFD('父模块不含有该权限，该模块也不能新增该权限！',-1);
        }
    }

    public function findAllChildren($id,&$res=[]){
        $temp = CrmSystemGsxx::findOne(['pid'=>$id]);
        if (!empty($temp)){
            $res[] = $temp;
            $this->findAllChildren($temp->id,$res);
        }else{
            return $res;
        }
    }

//    public function actionSave(){
//        $temp = CrmSystemGsxx::findOne(['pid'=>$id]);
//        if (!empty($temp)){
//            $res[] = $temp;
//            $this->findAllChildren($temp->id,$res);
//        }else{
//            return $res;
//        }
//    }
    public function findAllParents($pid,&$res=[]){
        $temp = CrmSystemGsxx::findOne(['id'=>$pid]);
        if (!empty($temp)){
            $res[] = $temp;
            $this->findAllParents($temp->pid,$res);
        }else{
            return $res;
        }
    }
}